package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.WorkerManAttendanceService;
import cn.buildoncloud.labor.workermanapplet.common.Constant;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import org.nutz.dao.entity.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/workerManAttendance")
public class WorkerManAttendanceController  {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WorkerManAttendanceService attendanceService;

    /**
     * 考勤管理-工友考勤列表
     * @param pId
     * @param search
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("/listPage")
    public Object listPage(@RequestParam String pId,
                           @RequestParam(required = false) String search,
                           @RequestParam(defaultValue = "1") Integer pageNumber,
                           @RequestParam(defaultValue = "10") int pageSize){
        try{
            Pagination pagination = attendanceService.listPage(Long.parseLong(pId), search, pageNumber, pageSize);
            return Result.success(Constant.API_OPERATION_SUCCESS, pagination);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }

    }

    @RequestMapping("/getWorkerMan")
    public Object getWorkerMan(@RequestParam String id){
        try{
            Map<String, Object> man =  attendanceService.getWorkerMan(Long.parseLong(id));
            return Result.success(Constant.API_OPERATION_SUCCESS, man);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    @RequestMapping("/getWorkerManAttendanceInfo")
    public Object getWorkerManAttendanceInfo(@RequestParam String pId,
                                             @RequestParam String wId,
                                             @RequestParam String date){
        try {
            List<Record> list = attendanceService.getWorkerManAttendanceInfo(Long.parseLong(pId),Long.parseLong(wId),date);

            return Result.success(Constant.API_OPERATION_SUCCESS, list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    @RequestMapping("/getTheDayAttendance")
    public Object getTheDayAttendance(@RequestParam String pId,
                                      @RequestParam String wId,
                                      @RequestParam String date){
        try{
            List<Record> list = attendanceService.getTheDayAttendance(Long.parseLong(pId),Long.parseLong(wId),date);
            return Result.success(Constant.API_OPERATION_SUCCESS, list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

}
